package com.ysten.basic.common.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Map;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.ysten.basic.common.utils.SystemConfigLoader;
import com.ysten.basic.common.utils.constant.Constant;
import com.ysten.basic.service.system.SystemDicService;

public abstract class BusinessBasicService
{
    
    @Autowired
    protected SystemConfigLoader systemConfigLoader;
    
    @Autowired
    protected DictionaryService dictionaryService;
    
    @Autowired
    private SystemDicService systemDicService;
    
    @Autowired
    private Cache cache;
    
    protected static final String DEFAULT_PATTERN = "yyyy-MM-dd";
    
    protected static DateFormat dateFormat = new SimpleDateFormat(DEFAULT_PATTERN);
    
    /**
     * 
     * <字典兑换><br />
     * <功能详细描述>
     *
     * @param entity 对象名
     * @param key 需兑换的key
     * @param separator 分隔符,可为空
     * @return
     * @see [类、类#方法、类#成员]
     */
    @SuppressWarnings("unchecked")
    public String getDictionaryValue(String entity, String key, String separator)
    {
        String str = "";
        StringBuffer sb = new StringBuffer();
        if (StringUtils.isBlank(separator))
        {
            separator = Constant.SEP_COMMA;
        }
        if (StringUtils.isNotBlank(entity) && StringUtils.isNotBlank(key))
        {
            Element element = cache.get(entity);
            Map<String, String> result = (Map<String, String>)element.getObjectValue();
            String[] keys = StringUtils.split(key, separator);
            for (String ks : keys)
            {
                String value = StringUtils.trimToEmpty(result.get(ks));
                if (StringUtils.isNotBlank(value))
                {
                    sb.append(value).append(",");
                }
            }
            if (StringUtils.isNotBlank(sb.toString()))
            {
                str = StringUtils.substring(sb.toString(), 0, sb.toString().length() - 1);
            }
        }
        return str;
    }
}
